package com.invengo.library_rfid.analyze.impl;

import static me.goldze.mvvmhabit.utils.constant.Constants.INVENGO_SHELF_V2;

import android.util.Log;

import com.invengo.library_rfid.analyze.ConvertUtil;
import com.invengo.library_rfid.analyze.EPCStrategy;
import com.invengo.library_rfid.analyze._EpcRuleTag;

import java.io.Serializable;
import java.util.Arrays;

public class ShanXiShiFanEPCStategyImpl implements EPCStrategy , Serializable {
    @Override
    public _EpcRuleTag getRule() {
        return _EpcRuleTag.RULE_ShanXiShiFan;
    }

    @Override
    public String analyzeShelf(String epc) {
        return getShelfCodeByEPC(epc, INVENGO_SHELF_V2);
    }

    @Override
    public String analyzeBook(String epc) {
        String barcode = ruleBy96Bit(epc);
        if (barcode.length()>8&&barcode.startsWith("0")) {
            barcode.replaceFirst("0","P");
        }
        return barcode;
    }

    public static String getShelfCodeByEPC(String epc,int[] SHELF_INTERVAL_V1) {
        String str = ConvertUtil.str16ToStr2(epc).substring(SHELF_INTERVAL_V1[0], SHELF_INTERVAL_V1[1]);
        int[] shelfInterval = Arrays.copyOfRange(SHELF_INTERVAL_V1,2, SHELF_INTERVAL_V1.length);
        int cursor =0;
        String result = "";
        for (int i : shelfInterval) {
            String format="%0"+(i/8+2)+"d";
            String res = String.format(format, Integer.parseInt(str.substring(cursor, i + cursor), 2));
            cursor+=i;
            result+=res;
        }
        return result;
    }
    public static String ruleBy96Bit(String epc) {
        String strEpc2 = ConvertUtil.str16ToStr2(epc);
        System.out.println(strEpc2);

        int barCodeLen = Integer.parseInt(strEpc2.substring(36, 40), 2);
        Log.d("条码长度：", barCodeLen + "");

        String barCode = "";
        char barCodeLast1 = (char) Integer.parseInt(strEpc2.substring(44, 48) + strEpc2.substring(32, 36), 2);
        Log.d("最后一位：", barCodeLast1 + "");

        char barCode3 = (char) (Integer.parseInt(strEpc2.substring(54, 56) + strEpc2.substring(40, 44), 2) + 48);
        Log.d("第三位：", barCode3 + "");

        char barCode2 = (char) (Integer.parseInt(strEpc2.substring(48, 54), 2) + 48);
        Log.d("第二位：", barCode2 + "");

        char barCode4 = (char) (Integer.parseInt(strEpc2.substring(70, 72) + strEpc2.substring(56, 58), 2) + 48);
        Log.d("第四位：", barCode4 + "");

        char barCode1 = (char) (Integer.parseInt(strEpc2.substring(59, 64), 2) + 48);
        Log.d("第一位：", barCode1 + "");

        String barCode5ToLast2 = "" + (Integer.parseInt(strEpc2.substring(88, 96) +
                strEpc2.substring(80, 88) +
                strEpc2.substring(72, 80) +
                strEpc2.substring(64, 70), 2));
        Log.d("第五位至倒数第二位：", barCode5ToLast2 + "");
        int i = barCodeLen - 5 - barCode5ToLast2.length();
        String barCode5T2 = "";
        if (i > 0) {
            for (int i1 = 0; i1 < i; i1++) {
                barCode5ToLast2 = "0" + barCode5ToLast2;
            }
            barCode5T2 = barCode5ToLast2;
        } else if (i < 0) {
            barCode5T2 = barCode5ToLast2.substring(0, barCodeLen - 5);
        } else {
            barCode5T2 = barCode5ToLast2;
        }
        return barCode + barCode1 + barCode2 + barCode3 + barCode4 + barCode5T2 + barCodeLast1;

    }
}
